<script setup>
import { h, onMounted, ref, resolveDirective, withDirectives } from 'vue'

import {
  useMessage,
  NButton,
  NForm,
  NFormItem,
  NInput,
  NInputNumber,
  NPopconfirm,
  NTreeSelect,
  NTag,
  NSwitch,
  NFlex,
  NAvatar,
  NSelect,
  NImage,
  NDataTable,
} from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudModal from '@/components/table/CrudModal.vue'
import CrudTable from '@/components/table/CrudTable.vue'
import TheIcon from '@/components/icon/TheIcon.vue'
import { getToken } from '@/utils'
import { renderIcon } from '@/utils'
import { useCRUD } from '@/composables'
// import { loginTypeMap, loginTypeOptions } from '@/constant/data'
import api from '@/api'
import { router } from '@/router'
import default_image from '@/assets/images/mz.jpg'

defineOptions({ name: '圈层' })
const message = useMessage();
const $table = ref(null)
const queryItems = ref({})
const vPermission = resolveDirective('permission')
const base_path = import.meta.env.VITE_BASE

const {
  modalVisible,
  modalTitle,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: '购物车',
  initForm: {},
  doCreate: api.createMiniLayers,
  doUpdate: api.updateMiniLayers,
  doDelete: api.deleteMiniLayers,
  refresh: () => $table.value?.handlerefresh(),
})
const isDisabled = ref(false)
const data = reactive({

})


onMounted(() => {
  $table.value?.handleSearch()
})

const columns = [
  {
    title: 'ID',
    key: 'id',
    width: 40,
    align: 'center',
    ellipsis: { tooltip: true },
  },

  {
    title: '用户',
    key: 'user_name',
    width: 60,
    align: 'center',
    ellipsis: { tooltip: true },
    fixed: 'left',
  },
    {
    title: '状态',
    key: 'status_name',
    width: 50,
    align: 'center',
    render(row) {
      return h(
          NTag,
          { type: 'primary', size: 'small', bordered: false, style: { margin: '2px 3px' } },
          { default: () => row.status_name }
      )
    },
  },
  {
    title: '图片',
    key: 'images',
    align: 'left',
    width:100,
    ellipsis: { tooltip: true },
    render(row) {
      const images = row.images ?? []
      const group = []
      for (let i = 0; i < images.length; i++)
        group.push(
            h(NImage,  { src: images[i].url,width: 40 ,height:40,style:"margin-right:2px;border-radius: 5px;"})
        )
      return h('span', group)
    },
  },
  {
    title: '标题',
    key: 'title',
    width: 60,
    align: 'center',
    ellipsis: { tooltip: true },
  },{
    title: '内容',
    key: 'content',
    width: 200,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '点赞',
    key: 'praise_count',
    width: 40,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '评论',
    key: 'review_count',
    width: 40,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '操作',
    key: 'actions',
    width: 80,
    align: 'center',
    fixed: 'right',
    render(row) {
      if(row.status==3){}else{
        return [
          withDirectives(
              h(
                  NButton,
                  {
                    size: 'small',
                    type: 'primary',
                    style: 'margin-right: 8px;',
                    onClick: () => {
                      handle_pass(row,1)
                    },
                  },
                  {
                    default: () => '通过',
                    icon: renderIcon('material-symbols:check', { size: 16 }),
                  }
              ),
              [[vPermission, 'post/api/content/contents/update']]
          ),
          h(
              NPopconfirm,
              {
                onPositiveClick: () => handle_pass(row,2),
                onNegativeClick: () => {},
              },
              {
                trigger: () =>
                    withDirectives(
                        h(
                            NButton,
                            {
                              size: 'small',
                              type: 'error',
                            },
                            {
                              default: () => '驳回',
                              icon: renderIcon('material-symbols:delete-outline', { size: 16 }),
                            }
                        ),
                        [[vPermission, 'delete/api/content/contents/delete']]
                    ),
                default: () => h('div', {}, '确定驳回该数据吗?'),
              }
          ),
        ]
      }

    },
  },
]

async function handle_pass(row,status) {
  row.status = status
  await api.updateMiniLayers(row)
  $table.value?.handleSearch()
  if(status===1){
    message.success('通过成功')
  }else{
    message.success('驳回成功')
  }

}
</script>

<template>
  <!-- 业务页面 -->
  <CommonPage show-footer title="圈层列表">
    <template #action>
      <div>

      </div>
    </template>
    <!-- 表格 -->
    <CrudTable
        ref="$table"
        v-model:query-items="queryItems"
        :columns="columns"
        :get-data="api.getMiniLayers"
        :scroll-x="1500"
    >
      <template #queryBar>
        <QueryBarItem label="客户姓名" :label-width="80">
          <NInput
              v-model:value="queryItems.user_name"
              clearable
              type="text"
              placeholder="请输入客户姓名"
              @keypress.enter="$table?.handleSearch()"
          />
        </QueryBarItem>
        <QueryBarItem label="标题" :label-width="80">
          <NInput
              v-model:value="queryItems.title"
              clearable
              type="text"
              placeholder="请输入标题"
              @keypress.enter="$table?.handleSearch()"
          />
        </QueryBarItem>
      </template>
    </CrudTable>
  </CommonPage>
</template>
